
export let protocolId = Buffer.from([
  0xfe,  // 254
  'S'.charCodeAt(0), //83
  'M'.charCodeAt(0), //77
  'B'.charCodeAt(0), //66
]);

const headerLength = 64;

/**
 * string,数字转换为buffer  小端字节序
 * @param {number} sessionId
 * @param {number} messageId
 * @param {number} command
 * @return {Array} 
 */
export let getHeaderSync = function(sessionId, messageId, command) {
  return [
    ['ProtocolId', 4, protocolId],  //固定值 (in network order) 0xFE, 'S', 'M', and 'B'.
    ['StructureSize', 2, headerLength],  //固定值
    ['CreditCharge', 2, 0], //  在SMB 2.0.2方言中，此字段不能使用，必须保留。发送方必须将其设置为0，接收方必须忽略它。在所有其他方言中字段指示此请求消耗的信用数。
    ['Status', 4, 0], //The client MUST set this field to 0 and the server MUST ignore it on receipt. In all SMB dialects for a response this field is interpreted as the Status field. This field can be set to any value. For a list of valid status codes, see [MS-ERREF]
   
    
    ['Command', 2, command],  //命令
    ['Credit', 2, 0], //在请求时，此字段指示客户端正在申请的信用数。在响应上，它表示授予客户的信用额度。
    
    /**
      SMB2_FLAGS_SERVER_TO_REDIR  0x00000001
        When set, indicates the message is a response, rather than a
        request. This MUST be set on responses sent from the server to the
        client and MUST NOT be set on requests sent from the client to the
        server.

      SMB2_FLAGS_ASYNC_COMMAND  0x00000002
        When set, indicates that this is an ASYNC SMB2 header. This flag
        MUST NOT be set when using the SYNC SMB2 header.
    
      SMB2_FLAGS_RELATED_OPERATIONS 0x00000004
        在请求和相应中都表示是一个复合请求链中的一个操作。

      SMB2_FLAGS_SIGNED 0x00000008
        When set, indicates that this packet has been signed. The use of
        this flag is as specified in section 3.1.5.1.

      SMB2_FLAGS_DFS_OPERATIONS
        0x10000000
        When set, indicates that this command is a DFS operation. 
    */
    
    
    ['Flags', 4, 0],

    //For a compounded request and response, this field MUST be set to the offset, in bytes, from the beginning of this SMB2 header to the start of the subsequent 8-byte aligned SMB2 header. If this is not a compounded request or response, or this is the last header in a compounded request or response, this value MUST be 0.
    ['NextCommand', 4, 0],
    ['MessageId', 8, messageId],
   // ['MessageIdHigh', 4, 0],  A value that identifies a message request and response uniquely across all messages that are sent on the same SMB 2 Protocol transport connection.
    
    ['Reserved', 4, 0], // 保留字段，固定0
    /**
     Uniquely identifies the tree connect for the command. This MUST be 0 for the
      SMB2 TREE_CONNECT Request. The TreeId can be any unsigned 32-bit integer that is received
      from a previous SMB2 TREE_CONNECT Response. TreeId SHOULD be set to 0 for the following
      commands:
       SMB2 NEGOTIATE Request
       SMB2 NEGOTIATE Response
       SMB2 SESSION_SETUP Request
       SMB2 SESSION_SETUP Response
       SMB2 LOGOFF Request
       SMB2 LOGOFF Response
       SMB2 ECHO Request
       SMB2 ECHO Response
       SMB2 CANCEL Request
     */
    ['TreeId', 4, 0],
    //Uniquely identifies the established session for the command. This field MUST be set to 0 for an SMB2 NEGOTIATE Request (section 2.2.3) and for an SMB2 NEGOTIATE Response (section 2.2.4).
    ['SessionId', 8, sessionId],
    //The 16-byte signature of the message, if SMB2_FLAGS_SIGNED is set in the Flags field of the SMB2 header and the message is not encrypted. If the message is not signed,this field MUST be 0.
    ['Signature', 16, 0],
  ];
};

var headerASync = function(processId, sessionId) {
  return [
    ['ProtocolId', 4, protocolId],
    ['StructureSize', 2, headerLength],
    ['CreditCharge', 2, 0],
    ['Status', 4, 0],
    ['Command', 2],
    ['Credit', 2, 126],
    ['Flags', 4, 0],
    ['NextCommand', 4, 0],
    ['MessageId', 4],
    ['MessageIdHigh', 4, 0],
    ['AsyncId', 8],
    ['SessionId', 8, sessionId],
    ['Signature', 16, 0],
  ];
};